package com.eminem.dao;

import com.eminem.common.StrMath;
import com.eminem.dao.base.BaseDao;
import com.eminem.model.Dept;
import com.eminem.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeptDao extends BaseDao<Dept> {
    public DeptDao() {
        super(Dept.class);
    }

    public List<Dept> findDeptAll(){
        List<Dept> deptList = new ArrayList<>();
        String sql = "select * from dept";
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            deptList = qr.query(sql, new BeanListHandler<>(Dept.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return deptList;
    }


    public List<Map<String, Object>> findDeptList() {
        List<Map<String, Object>> map = new ArrayList<>();
        String sql = "select d.id,d.name,ui.user_id,ui.real_name\n" +
                "from dept d\n" +
                "left join user u on u.dept_id = d.id\n" +
                "left join user_info ui on ui.user_id = u.id";
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());

        try {
            map = qr.query(sql, new MapListHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return map;

    }

    public List<Map<String, Object>> findSystemList(String userId, int currentPage, String name) {
        List<Map<String, Object>> map = new ArrayList<>();
        String sql = "select * from conference limit ?,?";
        if (null != name){
            sql = "select * from conference where title like ? limit ?,?";
        }
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            if (null != name){
                map = qr.query(sql,new MapListHandler(),"%"+name+"%",(currentPage - 1) * StrMath.PAGE_SIZE, StrMath.PAGE_SIZE);
            }else {
                map =  qr.query(sql,new MapListHandler(),(currentPage - 1) * StrMath.PAGE_SIZE, StrMath.PAGE_SIZE);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return map;
    }

    public long getCount(String name) {
        long count = 0L;
        String sql = "select count(*) as SystemCount from conference";
        if (null != name){
            sql = "select count(*) as SystemCount from conference where title like ?";
        }
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Map<String, Object> map = null;
        try {
            if (null != name){
                map = qr.query(sql,new MapHandler(),"%"+name+"%");
            }else {
                map = qr.query(sql,new MapHandler());
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (null != map){
            count = Long.parseLong(map.get("SystemCount").toString());
        }
        return count;
    }

    public List<Map<String, Object>> findDeptName() {
        List<Map<String, Object>> map = new ArrayList<>();
        String sql = "select * from dept";
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            map = qr.query(sql,new MapListHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return map;

    }

    public Map<String, Object> getSystemContent(String id) {
        Map<String, Object> map =new HashMap<>();
        String sql = "select * from conference";
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            map = qr.query(sql, new MapHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return map;
    }
}
